
//
//  tabbarViewController.m
//  card
//
//  Created by 韦显光 on 2017/5/19.
//  Copyright © 2017年 xg韦. All rights reserved.
//

#import "tabbarViewController.h"
#import "navViewController.h"
#import "nav2ViewController.h"
#import "nav3ViewController.h"
#import "nav4ViewController.h"
#import "nav5ViewController.h"
#import "MQChatViewManager.h"
@interface tabbarViewController ()<UITabBarControllerDelegate>
@property (nonatomic,assign) NSInteger  indexFlag;
@end
@implementation tabbarViewController



- (void)viewDidLoad {
    [super viewDidLoad];
     self.delegate=self;
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"shop" bundle:nil];
     UIStoryboard *mainStoryBoard2 = [UIStoryboard storyboardWithName:@"NewHome" bundle:nil];
     UIStoryboard *mainStoryBoard3 = [UIStoryboard storyboardWithName:@"dy" bundle:nil];
     UIStoryboard *mainStoryBoard4 = [UIStoryboard storyboardWithName:@"jzHD" bundle:nil];
     UIStoryboard *mainStoryBoard5 = [UIStoryboard storyboardWithName:@"map" bundle:nil];
    navViewController*one = [mainStoryBoard2 instantiateViewControllerWithIdentifier:@"nav1"];
    nav2ViewController*two = [mainStoryBoard instantiateViewControllerWithIdentifier:@"nav2"];
    nav3ViewController*san = [mainStoryBoard3 instantiateViewControllerWithIdentifier:@"nav3View"];
    nav4ViewController*si=[mainStoryBoard4 instantiateViewControllerWithIdentifier:@"nav4"];
    nav5ViewController*wu=[mainStoryBoard5 instantiateViewControllerWithIdentifier:@"nav5"];
    
           one.tabBarItem.image = [[UIImage imageNamed:@"首页未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           one.tabBarItem.selectedImage = [[UIImage imageNamed:@"首页选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           two.tabBarItem.image = [[UIImage imageNamed:@"薪资未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           two.tabBarItem.selectedImage = [[UIImage imageNamed:@"薪资选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           san.tabBarItem.image = [[UIImage imageNamed:@"消息未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           san.tabBarItem.selectedImage = [[UIImage imageNamed:@"消息选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           si.tabBarItem.image = [[UIImage imageNamed:@"我的未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           si.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    wu.tabBarItem.image = [[UIImage imageNamed:@"挖圈选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    wu.tabBarItem.selectedImage = [[UIImage imageNamed:@"挖圈选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
     one.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
     two.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
     san.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
     si.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
     wu.tabBarItem.imageInsets = UIEdgeInsetsMake(-10, 0, 10, 0);
    NSMutableDictionary *atts=[NSMutableDictionary dictionary];
    atts[NSFontAttributeName]=[UIFont systemFontOfSize:12];
    atts[NSForegroundColorAttributeName]=WXG_Color(153, 153, 153);
    [wu.tabBarItem setTitleTextAttributes:atts forState:UIControlStateSelected];
    [wu.tabBarItem setTitleTextAttributes:atts forState:UIControlStateNormal];
     [wu.tabBarItem setTitle:@"发现"];
     [two.tabBarItem setTitle:@"客服"];
    NSMutableDictionary *atts2=[NSMutableDictionary dictionary];
    atts2[NSFontAttributeName]=[UIFont systemFontOfSize:12];
    atts2[NSForegroundColorAttributeName]=WXG_Color(153, 153, 153);
    [two.tabBarItem setTitleTextAttributes:atts2 forState:UIControlStateSelected];
    [two.tabBarItem setTitleTextAttributes:atts2 forState:UIControlStateNormal];
//    NSArray *item = [NSArray arrayWithObjects:one,two,san,si,nil];
//    [self setViewControllers:item];
    [self addChildViewController:one];
    [self addChildViewController:two];
    [self addChildViewController:wu];
    [self addChildViewController:san];
    [self addChildViewController:si];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != self.indexFlag) {
        //执行动画
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.tabBar.subviews) {
            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [arry addObject:btn];
            }
        }
        //添加动画
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //速度控制函数，控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.2;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
        animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
        self.indexFlag = index;
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([viewController.tabBarItem.title isEqualToString:@"客服"]){
        MQChatViewManager *chatViewManager = [[MQChatViewManager alloc] init];
        [chatViewManager setoutgoingDefaultAvatarImage:[UIImage imageNamed:@"meiqia-icon"]];
        [chatViewManager pushMQChatViewControllerInViewController:self];
        return NO;
    }
    return YES;
}
@end
